package com.tour.controller;


import com.tour.config.ResponseWrapper;
import com.tour.entity.Commodity;
import com.tour.mapper.CommodityMapper;
import com.tour.service.impl.CommodityServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lel
 * @since 2021-03-30
 */
@RestController
@Api(tags = "土特产商品")
public class CommodityController {

    @Autowired
    CommodityServiceImpl commodityService;

    @ApiOperation("增加商品")
    @PostMapping("/addCommodity")
    public ResponseWrapper addCommodity(@RequestBody Commodity commodity){
        ResponseWrapper wrapper = commodityService.addCommodity(commodity);
        return wrapper;
    }

    @ApiOperation("删除某个商品")
    @ApiImplicitParam(name = "cid",value = "商品id")
    @DeleteMapping("/deleteCommodity")
    public ResponseWrapper deleteCommodity(@RequestParam(name = "cid")Integer cid){
        ResponseWrapper wrapper = commodityService.deleteCommodity(cid);
        return wrapper;
    }

    @ApiOperation("修改商品")
    @PutMapping("/updateCommodity")
    public ResponseWrapper updateCommodity(@RequestBody Commodity commodity){
        ResponseWrapper wrapper = commodityService.updateCommodity(commodity);
        return wrapper;
    }

    @ApiOperation("查看某一个商品")
    @ApiImplicitParam(name = "cid",value = "商品id")
    @GetMapping("/app/findCommodity")
    public ResponseWrapper findCommodity(@RequestParam(name = "cid")Integer cid){
        ResponseWrapper wrapper = commodityService.findCommodity(cid);
        return wrapper;
    }

    @ApiOperation("查看所有商品或者模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页展示的数据条数",defaultValue = "10"),
            @ApiImplicitParam(name = "pageNum",value = "页码数",defaultValue = "1"),
            @ApiImplicitParam(name = "name",value = "商品名字"),
            @ApiImplicitParam(name = "place",value = "产地")
    })
    @GetMapping("/app/findAllCommodity")
    public ResponseWrapper findAllCommodity(@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                                       @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                                       @RequestParam(name = "name",required = false)String name,
                                       @RequestParam(name = "place",required = false)String place){
        ResponseWrapper wrapper = commodityService.findAllCommodity(name,pageSize,pageNum,place);
        return wrapper;
    }
}

